package edu.metrostate.capstonediner;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Looper;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;

public class AppStoreActivity extends Activity {
	
	Button btnCreatePayment;
	Button btnPurchase1;
	Button btnPurchase2;
	
	private AppStoreActivity myInstance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_store);
		// Show the Up button in the action bar.
		setupActionBar();
		
		myInstance = this;
		
		btnCreatePayment = (Button) findViewById(R.id.PaymentButton);
		btnPurchase1 = (Button) findViewById(R.id.purchase1Button);
		btnPurchase2 = (Button) findViewById(R.id.purchase2Button);
		
		// Create Payment
		btnCreatePayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	if (GameActivity.getUser() != null) {
	                //  This will start the InAppStore
	                Intent i = new Intent(getApplicationContext(), PaymentActivity.class);
	                startActivity(i);
            	} else {
            		AlertDialog.Builder builder = new AlertDialog.Builder(myInstance);
            		builder.setMessage("Please log in before setting up a payment method.");
		            builder.create().show();
            	}
 
            }
        });
		
		btnPurchase1.setOnClickListener(new purchaseButtonListener(1));
		btnPurchase2.setOnClickListener(new purchaseButtonListener(2));
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.app_store, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private class purchaseButtonListener implements View.OnClickListener, Runnable {
		int itemID;
		
		public purchaseButtonListener(int itemID) {
			this.itemID = itemID;
		}
		
		@Override
		public void onClick(View v) {
			new Thread(this).start();
		}
		
		@Override
		public void run() {
			String user = GameActivity.getUser();
			
			Looper.prepare();
	    	AlertDialog.Builder builder = new AlertDialog.Builder(myInstance);
			
			if (user != null)  {
				if (DatabaseHTTP.VerifyPaymentSetup(user)) {
					if (!DatabaseHTTP.VerifyPurchase(user, Integer.toString(itemID))) {
						// confirm purchase
						builder.setMessage("Are you sure that you want to make this purchase?");
						builder.setPositiveButton("Yes, Absolutely!", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								DatabaseHTTP.SavePurchase(GameActivity.getUser(), Integer.toString(itemID));
							}
						});
						builder.setNegativeButton("Changed my mind", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// User cancelled the purchase
							}
						});
						builder.create().show();
						Looper.loop();
					} else {
						// previously purchased
						builder.setMessage("You already own that upgrade.");
			            builder.create().show();
			            Looper.loop();
					}
				} else {
					builder.setMessage("Please setup a paymenth method before making a purchase.");
		            builder.create().show();
		            Looper.loop();
				}
			} else {
				builder.setMessage("Please log in before making a purchase.");
	            builder.create().show();
	            Looper.loop();
			}
		}
	}
}
